//
//  BaseNavigationController.m
//  SYDrive
//
//  Created by CleverPeng on 2020/6/9.
//  Copyright © 2020 cmss. All rights reserved.
//

#import "BaseNavigationController.h"

@interface BaseNavigationController () <UINavigationControllerDelegate, UIGestureRecognizerDelegate>

@end

@implementation BaseNavigationController

+ (void)initialize {
    //appearance方法返回一个导航栏的外观对象
    //修改了这个外观对象，相当于修改了整个项目中的外观
    UINavigationBar *navigationBar = [UINavigationBar appearance];

    //设置导航栏背景颜色
    if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance *appearence = [[UINavigationBarAppearance alloc] init];
        NSDictionary *dic = @{NSForegroundColorAttributeName: [UIColor colorWithRed:31.0 / 255 green:45.0 / 255 blue:61.0 / 255 alpha:1], NSFontAttributeName : [UIFont systemFontOfSize:18 weight:UIFontWeightMedium]};
        appearence.titleTextAttributes =dic;
        appearence.backgroundColor = UIColor.whiteColor;
        navigationBar.standardAppearance = appearence;
        navigationBar.scrollEdgeAppearance = appearence;
    } else {
        [navigationBar setBarTintColor:UIColor.whiteColor];
        NSDictionary *dic = @{NSForegroundColorAttributeName: [UIColor colorWithRed:31.0 / 255 green:45.0 / 255 blue:61.0 / 255 alpha:1], NSFontAttributeName : [UIFont systemFontOfSize:18 weight:UIFontWeightMedium]};
        navigationBar.titleTextAttributes =dic;
    }

    //设置标题栏颜色
    navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor colorWithRed:31.0 / 255 green:45.0 / 255 blue:61.0 / 255 alpha:1], NSFontAttributeName : [UIFont systemFontOfSize:18 weight:UIFontWeightMedium]};
}

- (void)viewDidLoad {
    [super viewDidLoad];
    __weak typeof(self) wkself = self;
    self.delegate = wkself;
    self.interactivePopGestureRecognizer.delegate = wkself;
}

///MARK: override
//全部修改返回按钮,但是会失去右滑返回的手势
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
        [viewController setUpNavigationLeftItem];
    }

    [super pushViewController:viewController animated:animated];
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
    UIViewController *viewController = [super popViewControllerAnimated:animated];
    __weak typeof(self) wkself = self;
    self.delegate = wkself;
    return viewController;
}

///MARK: UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if ([viewController needHideNavigationBar]) {
        [navigationController setNavigationBarHidden:YES animated:YES];
    }else {
        [navigationController setNavigationBarHidden:NO animated:YES];
    }
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if (self.viewControllers.count > 1) {
        return YES;
    } else {
        return NO;
    }
}



@end
